package com.vids.controller;

import cn.hutool.core.collection.CollUtil;
import com.vids.utils.StringUtils;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.vids.constant.Urlconstant;
import com.vids.constant.VIIDRespEnum;
import com.vids.model.entity.ImageBusiness;
import com.vids.model.entity.NonMotorVehicle;
import com.vids.model.param.NonMotorVehicleBatchParam;
import com.vids.model.param.NonMotorVehicleModel;
import com.vids.model.param.NonMotorVehicleParam;
import com.vids.model.param.SubImageBatchParam;
import com.vids.model.response.NonMotorVehicleBatch;
import com.vids.model.response.ResponseStatus;
import com.vids.model.vo.SubImageInfoVo;
import com.vids.service.IImageBusinessService;
import com.vids.service.INonMotorVehicleService;
import com.vids.utils.BeanUtilss;
import com.vids.utils.BussinessIDGennelUtil;
import com.vids.utils.ImageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;

/**
 * <p>
 * 机动车 前端控制器
 * </p>
 */
@Slf4j
@RestController
@RequestMapping("/VIID")
@RequiredArgsConstructor
public class NonMotorVehicleController
{

    public final static String fCode = "02";
    public final static String cCode = "02";

    public final static String type = "01";

    private String appId = "42010000005040000000";

    private final INonMotorVehicleService nonMotorVehicleService;

    private final IImageBusinessService imageBusinessService;


    /**
     * 校验参数
     *
     * @param status
     */
    private void validateParams(NonMotorVehicle tBigDataNonMotorVehicle, ResponseStatus status) {
        StringBuffer sb = new StringBuffer();
        if (StringUtils.isEmpty(tBigDataNonMotorVehicle.getNonMotorVehicleID())) {
            sb.append("NonMotorVehicleID必填；");
        }
        if (tBigDataNonMotorVehicle.getInfoKind() == null) {
            sb.append("InfoKind必填；");
        }
        if (StringUtils.isEmpty(tBigDataNonMotorVehicle.getSourceID())) {
            sb.append("SourceID必填；");
        }
//        if (StringUtils.isEmpty(tBigDataNonMotorVehicle.getStorageUrl())) {
//            sb.append("StorageUrl1必填；");
//        }
        if (StringUtils.isEmpty(tBigDataNonMotorVehicle.getVehicleColor())) {
            sb.append("VehicleColor必填；");
        }
        if (tBigDataNonMotorVehicle.getInfoKind() != null && tBigDataNonMotorVehicle.getInfoKind() == 1) {
            if (StringUtils.isEmpty(tBigDataNonMotorVehicle.getDeviceID())) {
                sb.append("自动采集时，DeviceID必填；");
            }
            if (tBigDataNonMotorVehicle.getLeftTopX() == null) {
                sb.append("自动采集时，LeftTopX必填；");
            }
            if (tBigDataNonMotorVehicle.getLeftTopY() == null) {
                sb.append("自动采集时，LeftTopY必填；");
            }
            if (tBigDataNonMotorVehicle.getRightBtmX() == null) {
                sb.append("自动采集时，RightBtmX必填；");
            }
            if (tBigDataNonMotorVehicle.getRightBtmY() == null) {
                sb.append("自动采集时，RightBtmY必填；");
            }
        }
        if (tBigDataNonMotorVehicle.getLeftTopX() != null && !tBigDataNonMotorVehicle.getLeftTopX().toString()
            .matches("^[0-9]+$")) {
            sb.append("LeftTopX字段为数字；");
        }
        if (tBigDataNonMotorVehicle.getLeftTopY() != null && !tBigDataNonMotorVehicle.getLeftTopY().toString()
            .matches("^[0-9]+$")) {
            sb.append("LeftTopY字段为数字；");
        }
        if (tBigDataNonMotorVehicle.getRightBtmX() != null && !tBigDataNonMotorVehicle.getRightBtmX().toString()
            .matches("^[0-9]+$")) {
            sb.append("RightBtmX字段为数字；");
        }
        if (tBigDataNonMotorVehicle.getRightBtmY() != null && !tBigDataNonMotorVehicle.getRightBtmY().toString()
            .matches("^[0-9]+$")) {
            sb.append("RightBtmY字段为数字；");
        }
        if (tBigDataNonMotorVehicle.getInfoKind() != null && tBigDataNonMotorVehicle.getInfoKind() == 2) {
            if (tBigDataNonMotorVehicle.getMarkTime() == null) {
                sb.append("人工采集时，MarkTime必填；");
            }
            if (tBigDataNonMotorVehicle.getAppearTime() == null) {
                sb.append("人工采集时，AppearTime必填；");
            }
            if (tBigDataNonMotorVehicle.getDisappearTime() == null) {
                sb.append("人工采集时，DisappearTime必填；");
            }
        }
        if (!StringUtils.isEmpty(tBigDataNonMotorVehicle.getNonMotorVehicleID())) {
            if (tBigDataNonMotorVehicle.getNonMotorVehicleID().length() != 48) {
                sb.append("NonMotorVehicleID字段长度不为48；");
            }
            if (!tBigDataNonMotorVehicle.getNonMotorVehicleID().matches("^[0-9]+$")) {
                sb.append("NonMotorVehicleID字段为纯数字；");
            }
        }
        if (!StringUtils.isEmpty(tBigDataNonMotorVehicle.getSourceID())) {
            if (tBigDataNonMotorVehicle.getSourceID().length() != 41) {
                sb.append("SourceID字段长度不为41；");
            }
            if (!tBigDataNonMotorVehicle.getSourceID().matches("^[0-9]+$")) {
                sb.append("SourceID字段为纯数字；");
            }
        }
        if (!StringUtils.isEmpty(tBigDataNonMotorVehicle.getDeviceID())) {
            if (tBigDataNonMotorVehicle.getDeviceID().length() != 20) {
                sb.append("DeviceID字段长度不为20；");
            }
            if (!tBigDataNonMotorVehicle.getDeviceID().matches("^[0-9]+$")) {
                sb.append("DeviceID字段为纯数字；");
            }
        }
//        if (!StringUtils.isEmpty(tBigDataNonMotorVehicle.getTollgateID())) {
//            if (tBigDataNonMotorVehicle.getTollgateID().length() != 20) {
//                sb.append("TollgateID字段长度不为20；");
//            }
//            if (!tBigDataNonMotorVehicle.getTollgateID().matches("^[0-9]+$")) {
//                sb.append("TollgateID字段为纯数字；");
//            }
//        }
//        if (!StringUtils.isEmpty(tBigDataNonMotorVehicle.getStorageUrl1()) && tBigDataNonMotorVehicle.getStorageUrl1()
//            .length() > 256) {
//            sb.append("StorageUrl1字段长度不能大于256；");
//        }
//        if (!StringUtils.isEmpty(tBigDataNonMotorVehicle.getStorageUrl2()) && tBigDataNonMotorVehicle.getStorageUrl2()
//            .length() > 256) {
//            sb.append("StorageUrl2字段长度不能大于256；");
//        }
//        if (!StringUtils.isEmpty(tBigDataNonMotorVehicle.getStorageUrl3()) && tBigDataNonMotorVehicle.getStorageUrl3()
//            .length() > 256) {
//            sb.append("StorageUrl3字段长度不能大于256；");
//        }
//        if (!StringUtils.isEmpty(tBigDataNonMotorVehicle.getStorageUrl4()) && tBigDataNonMotorVehicle.getStorageUrl4()
//            .length() > 256) {
//            sb.append("StorageUrl4字段长度不能大于256；");
//        }
//        if (!StringUtils.isEmpty(tBigDataNonMotorVehicle.getStorageUrl5()) && tBigDataNonMotorVehicle.getStorageUrl5()
//            .length() > 256) {
//            sb.append("StorageUrl5字段长度不能大于256；");
//        }
        if (!StringUtils.isEmpty(tBigDataNonMotorVehicle.getPlateDescribe()) && tBigDataNonMotorVehicle.getPlateDescribe()
            .length() > 64) {
            sb.append("PlateDescribe字段长度不能大于64；");
        }
//        if (!StringUtils.isEmpty(tBigDataNonMotorVehicle.getVehicleStyles()) && tBigDataNonMotorVehicle.getVehicleStyles()
//            .length() > 16) {
//            sb.append("VehicleStyles字段长度不能大于16；");
//        }
        if (!StringUtils.isEmpty(tBigDataNonMotorVehicle.getVehicleHood()) && tBigDataNonMotorVehicle.getVehicleHood()
            .length() > 64) {
            sb.append("VehicleHood字段长度不能大于64；");
        }
        if (!StringUtils.isEmpty(tBigDataNonMotorVehicle.getVehicleTrunk()) && tBigDataNonMotorVehicle.getVehicleTrunk()
            .length() > 64) {
            sb.append("VehicleTrunk字段长度不能大于64；");
        }
        if (!StringUtils.isEmpty(tBigDataNonMotorVehicle.getVehicleWheel()) && tBigDataNonMotorVehicle.getVehicleWheel()
            .length() > 64) {
            sb.append("VehicleWheel字段长度不能大于64；");
        }
        if (!StringUtils.isEmpty(tBigDataNonMotorVehicle.getVehicleWindow()) && tBigDataNonMotorVehicle.getVehicleWindow()
            .length() > 64) {
            sb.append("VehicleWindow字段长度不能大于64；");
        }
        if (!StringUtils.isEmpty(tBigDataNonMotorVehicle.getVehicleRoof()) && tBigDataNonMotorVehicle.getVehicleRoof()
            .length() > 64) {
            sb.append("VehicleRoof字段长度不能大于64；");
        }
        if (!StringUtils.isEmpty(tBigDataNonMotorVehicle.getVehicleDoor()) && tBigDataNonMotorVehicle.getVehicleDoor()
            .length() > 64) {
            sb.append("VehicleDoor字段长度不能大于64；");
        }
        if (!StringUtils.isEmpty(tBigDataNonMotorVehicle.getSideOfVehicle()) && tBigDataNonMotorVehicle.getSideOfVehicle()
            .length() > 64) {
            sb.append("SideOfVehicle字段长度不能大于64；");
        }
        if (!StringUtils.isEmpty(tBigDataNonMotorVehicle.getCarOfVehicle()) && tBigDataNonMotorVehicle.getCarOfVehicle()
            .length() > 64) {
            sb.append("CarOfVehicle字段长度不能大于64；");
        }
        if (!StringUtils.isEmpty(tBigDataNonMotorVehicle.getRearviewMirror()) && tBigDataNonMotorVehicle.getRearviewMirror()
            .length() > 64) {
            sb.append("RearviewMirror字段长度不能大于64；");
        }
        if (!StringUtils.isEmpty(tBigDataNonMotorVehicle.getVehicleChassis()) && tBigDataNonMotorVehicle.getVehicleChassis()
            .length() > 64) {
            sb.append("VehicleChassis字段长度不能大于64；");
        }
        if (!StringUtils.isEmpty(
            tBigDataNonMotorVehicle.getVehicleShielding()) && tBigDataNonMotorVehicle.getVehicleShielding().length() > 64) {
            sb.append("VehicleShielding字段长度不能大于64；");
        }
//        if (!StringUtils.isEmpty(tBigDataNonMotorVehicle.getVehicleBodyDesc()) && tBigDataNonMotorVehicle.getVehicleBodyDesc()
//            .length() > 128) {
//            sb.append("PlateDescribe字段长度不能大于128；");
//        }
//        if (!StringUtils.isEmpty(tBigDataNonMotorVehicle.getDescOfFrontItem()) && tBigDataNonMotorVehicle.getDescOfFrontItem()
//            .length() > 256) {
//            sb.append("DescOfFrontItem字段长度不能大于256；");
//        }
//        if (!StringUtils.isEmpty(tBigDataNonMotorVehicle.getDescOfRearItem()) && tBigDataNonMotorVehicle.getDescOfRearItem()
//            .length() > 256) {
//            sb.append("DescOfRearItem字段长度不能大于256；");
//        }
//        if (!StringUtils.isEmpty(
//            tBigDataNonMotorVehicle.getNameOfPassedRoad()) && tBigDataNonMotorVehicle.getNameOfPassedRoad().length() > 64) {
//            sb.append("NameOfPassedRoad字段长度不能大于64；");
//        }
//        if (!StringUtils.isEmpty(
//            tBigDataNonMotorVehicle.getPlateReliability()) && tBigDataNonMotorVehicle.getPlateReliability().length() > 3) {
//            sb.append("PlateReliability字段长度不能大于3；");
//        }
//        if (!StringUtils.isEmpty(
//            tBigDataNonMotorVehicle.getPlateCharReliability()) && tBigDataNonMotorVehicle.getPlateCharReliability()
//            .length() > 64) {
//            sb.append("PlateCharReliability字段长度不能大于64；");
//        }
//        if (!StringUtils.isEmpty(
//            tBigDataNonMotorVehicle.getBrandReliability()) && tBigDataNonMotorVehicle.getBrandReliability().length() > 3) {
//            sb.append("BrandReliability字段长度不能大于3；");
//        }

        if (!StringUtils.isEmpty(
            tBigDataNonMotorVehicle.getUsingPropertiesCode()) && !tBigDataNonMotorVehicle.getUsingPropertiesCode()
            .matches("^[A-R]|Z+$")) {
            sb.append("UsingPropertiesCode的值不合法[[A-R]|Z]；");
        }
//        if (!StringUtils.isEmpty(tBigDataNonMotorVehicle.getNonVehicleClass()) && !tBigDataNonMotorVehicle.getNonVehicleClass()
//            .matches("^[KHQZDMNTJGBX]\\d{2}+$")) {
//            sb.append("VehicleClass的值不合法[[KHQZDMNTJGBX]\\d{2}];");
//        }

        // PlateNo 15
        if (!StringUtils.isEmpty(tBigDataNonMotorVehicle.getPlateNo()) && tBigDataNonMotorVehicle.getPlateNo()
            .length() > 15) {
            sb.append("PlateNo字段长度不能大于15；");
        }
        // PlateNoAttach 15
        if (!StringUtils.isEmpty(tBigDataNonMotorVehicle.getPlateNoAttach()) && tBigDataNonMotorVehicle.getPlateNoAttach()
            .length() > 15) {
            sb.append("PlateNoAttach字段长度不能大于15；");
        }
        // DrivingStatusCode =4
        if (!StringUtils.isEmpty(
            tBigDataNonMotorVehicle.getDrivingStatusCode()) && tBigDataNonMotorVehicle.getDrivingStatusCode().length() != 2) {
            sb.append("DrivingStatusCode字段长度不为2；");
        }
        // WheelPrintedPattern =2
        if (!StringUtils.isEmpty(
            tBigDataNonMotorVehicle.getWheelPrintedPattern()) && tBigDataNonMotorVehicle.getWheelPrintedPattern()
            .length() != 2) {
            sb.append("WheelPrintedPattern字段长度不为2；");
        }
        // VehicleModel 32
//        if (!StringUtils.isEmpty(tBigDataNonMotorVehicle.getNonVehicleModel()) && tBigDataNonMotorVehicle.getNonVehicleModel()
//            .length() > 32) {
//            sb.append("VehicleModel字段长度不能大于32；");
//        }
        // VehicleLength 5位整数，
        if (tBigDataNonMotorVehicle.getVehicleLength() != null && !(tBigDataNonMotorVehicle.getVehicleLength() + "").matches(
            "^[0-9]{5}$")) {
            sb.append("VehicleLength字段5位整数；");
        }
        // VehicleWidth 4位整数，
        if (tBigDataNonMotorVehicle.getVehicleWidth() != null && !(tBigDataNonMotorVehicle.getVehicleWidth() + "").matches(
            "^[0-9]{4}$")) {
            sb.append("VehicleWidth字段5位整数；");
        }
        // VehicleHeight 4位整数，
        if (tBigDataNonMotorVehicle.getVehicleHeight() != null && !(tBigDataNonMotorVehicle.getVehicleHeight() + "").matches(
            "^[0-9]{4}$")) {
            sb.append("VehicleLength字段5位整数；");
        }
        // Sunvisor 0：收起；1：放下
//        if (tBigDataNonMotorVehicle.getSunvisor() != null && !Arrays.asList(0, 1)
//            .contains(tBigDataNonMotorVehicle.getSunvisor())) {
//            sb.append("Sunvisor的值不正确，Sunvisor 0：收起；1：放下；");
//        }
//        // SafetyBelt 0：未系；1：有系
//        if (tBigDataNonMotorVehicle.getSafetyBelt() != null && !Arrays.asList(0, 1)
//            .contains(tBigDataNonMotorVehicle.getSafetyBelt())) {
//            sb.append("SafetyBelt的值不正确，SafetyBelt 0：未系；1：有系；");
//        }
//        // Calling 0：未打电话；1：打电话中
//        if (tBigDataNonMotorVehicle.getCalling() != null && !Arrays.asList(0, 1)
//            .contains(tBigDataNonMotorVehicle.getCalling())) {
//            sb.append("Calling的值不正确，Calling 0：未打电话；1：打电话中；");
//        }

        if (sb != null && !StringUtils.isEmpty(sb.toString())) {
            setStatus(status, VIIDRespEnum.INVALID_JSON_CONTENT, sb.toString());
        }
    }

    /**
     * 批量新增
     *
     * @param param
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/NonMotorVehicles", produces = "application/VIID+JSON")
    public Object postList(@RequestBody NonMotorVehicleBatchParam param) throws Exception {
        List<ResponseStatus> resps = new ArrayList<ResponseStatus>();
        if (param == null || param.getNonMotorVehicleListObject() == null || param.getNonMotorVehicleListObject()
            .getNonMotorVehicleObject() == null || param.getNonMotorVehicleListObject().getNonMotorVehicleObject().size() == 0) {
            ResponseStatus status =
                new ResponseStatus(Urlconstant.URL_BIG_DATA_19, VIIDRespEnum.OK.getCode(), new Date());
            setStatus(status, VIIDRespEnum.INVALID_JSON_CONTENT, "参数为空；");
            resps.add(status);
            return new ResponseEntity<Map<String, Object>>(
                BeanUtilss.objectToMap(BeanUtilss.getBatchResponseModel(resps)), HttpStatus.CREATED);
        }
        List<NonMotorVehicleModel> list = param.getNonMotorVehicleListObject().getNonMotorVehicleObject();

        if (CollUtil.isNotEmpty(list)) {

            log.info("保存机动车图片开始");
            //TODO:这里把数据接到公司对应的接口
            //TransactionReceipt transactionReceipt = carCallbackMock.getResult();   // 部署合约的回执信息
            for (NonMotorVehicleModel NonMotorVehicleModel : list) {
                //把抓拍车辆图片写到本地
                List<SubImageInfoVo> subImageInfoObject = NonMotorVehicleModel.getSubImageList().getSubImageInfoObject();
                log.info("Vehicle Data=======: "+subImageInfoObject.get(0).getData());

                for (SubImageInfoVo subImageInfoVo: subImageInfoObject)
                {
                    log.info("NOMOZMC"+subImageInfoVo.getImageID());
                    ImageUtil.convertBase64ToImageAndSave(subImageInfoVo.getData(), NonMotorVehicleModel.getNonMotorVehicleID(),
                                                   subImageInfoVo.getImageID(),"non_motor");
                }

            }

            log.info("保存机动车图片结束");
        }

//        commonSaveCarBatchModel(resps, list);
        return new ResponseEntity<Map<String, Object>>(BeanUtilss.objectToMap(BeanUtilss.getBatchResponseModel(resps)),
            HttpStatus.CREATED);
    }

    public void commonSaveCarBatchModel(List<ResponseStatus> resps, List<NonMotorVehicleModel> list) {
        for (NonMotorVehicleModel model : list) {
            ResponseStatus status =
                new ResponseStatus(Urlconstant.URL_BIG_DATA_19, VIIDRespEnum.OK.getCode(), new Date());

            if (model == null) {
                setStatus(status, VIIDRespEnum.INVALID_JSON_CONTENT, "参数为空；");
                resps.add(status);
                continue;
            }

            NonMotorVehicleBatch NonMotorVehicle = BeanUtilss.transferObjectCommon(model, NonMotorVehicleBatch.class);
            if (model.getSubImageList() != null && model.getSubImageList()
                .getSubImageInfoObject() != null && model.getSubImageList().getSubImageInfoObject().size() > 0) {
                NonMotorVehicle.setSubImageList(model.getSubImageList().getSubImageInfoObject());
            }

            try {
                if (NonMotorVehicle != null && StringUtils.isEmpty(
                    NonMotorVehicle.getNonMotorVehicleID()) && NonMotorVehicle.getInfoKind() != null && NonMotorVehicle.getInfoKind() == 1) {
                    NonMotorVehicle.setNonMotorVehicleID(
                        BussinessIDGennelUtil.createViewLibraryID(appId, fCode, true, cCode));
                } else {
                    NonMotorVehicle TBigDataNonMotorVehicle2 =
                        nonMotorVehicleService.findByNonMotorVehicleID(NonMotorVehicle.getNonMotorVehicleID());
                    if (TBigDataNonMotorVehicle2 != null) {
                        setStatus(status, VIIDRespEnum.INVALID_JSON_CONTENT, "参数NonMotorVehicleID重复；");
                        resps.add(status);
                        continue;
                    }
                }

                validateParams(NonMotorVehicle, status);
                if (status.getStatusCode().equals(VIIDRespEnum.OK.getCode())) {
                    if (status.getStatusCode().equals(VIIDRespEnum.OK.getCode())) {
                        NonMotorVehicle tBigDataNonMotorVehicle =
                            BeanUtilss.transferObjectIgnoreCase(NonMotorVehicle, NonMotorVehicle.class);
                        nonMotorVehicleService.save(tBigDataNonMotorVehicle);

                        // 图片和业务对于关系
                        saveImageBusiness(NonMotorVehicle.getSubImageList(), type, NonMotorVehicle.getNonMotorVehicleID());
                        status.setId(NonMotorVehicle.getNonMotorVehicleID());
                        status.setStatusString("新增成功；");
                    }
                }
            } catch (Exception e) {
                setStatus(status, VIIDRespEnum.DEVICE_ERROR, "请联系管理员；");
                log.error(" TBigDataNonMotorVehicleController  postList  Exception {} ", e);
            }
            resps.add(status);
        }
    }

    /**
     * 批量修改
     *
     * @param param
     * @return
     * @throws IOException
     * @throws JsonMappingException
     * @throws JsonParseException
     */
    @PutMapping(value = "/NonMotorVehicles", produces = "application/VIID+JSON")
    public Object putList(@RequestBody NonMotorVehicleBatchParam param) throws IOException {
        List<ResponseStatus> resps = new ArrayList<ResponseStatus>();
        if (param == null || param.getNonMotorVehicleListObject() == null || param.getNonMotorVehicleListObject()
            .getNonMotorVehicleObject() == null || param.getNonMotorVehicleListObject().getNonMotorVehicleObject().size() == 0) {
            ResponseStatus status =
                new ResponseStatus(Urlconstant.URL_BIG_DATA_19, VIIDRespEnum.OK.getCode(), new Date());
            setStatus(status, VIIDRespEnum.INVALID_JSON_CONTENT, "参数为空；");
            resps.add(status);
            return BeanUtilss.getBatchResponseModel(resps);
        }

        List<NonMotorVehicleModel> list = param.getNonMotorVehicleListObject().getNonMotorVehicleObject();
        commonPutCarBatchModel(resps, list);
        return BeanUtilss.getBatchResponseModel(resps);
    }

    public void commonPutCarBatchModel(List<ResponseStatus> resps, List<NonMotorVehicleModel> list) {
        for (NonMotorVehicleModel model : list) {
            ResponseStatus status =
                new ResponseStatus(Urlconstant.URL_BIG_DATA_19, VIIDRespEnum.OK.getCode(), new Date());
            if (model == null) {
                setStatus(status, VIIDRespEnum.INVALID_JSON_CONTENT, "参数为空；");
                resps.add(status);
                continue;
            }

            NonMotorVehicleBatch NonMotorVehicle = BeanUtilss.transferObjectCommon(model, NonMotorVehicleBatch.class);
            if (model.getSubImageList() != null && model.getSubImageList()
                .getSubImageInfoObject() != null && model.getSubImageList().getSubImageInfoObject().size() > 0) {
                NonMotorVehicle.setSubImageList(model.getSubImageList().getSubImageInfoObject());
            }

            try {
                validateParams(NonMotorVehicle, status);
                if (status.getStatusCode().equals(VIIDRespEnum.OK.getCode())) {
                    updateCommon(NonMotorVehicle, status, NonMotorVehicle.getNonMotorVehicleID());
                }
            } catch (Exception e) {
                setStatus(status, VIIDRespEnum.DEVICE_ERROR, "请联系管理员；");
                log.error(" TBigDataNonMotorVehicleController  putList  Exception {} ", e);
            }
            resps.add(status);
        }
    }

    /**
     * 批量删除
     *
     * @param IDList
     * @return
     */
    @DeleteMapping(value = "/NonMotorVehicles", produces = "application/VIID+JSON")
    public Object deleteList(@RequestParam("IDList") String IDList) {
        List<ResponseStatus> resps = new ArrayList<ResponseStatus>();
        if (StringUtils.isEmpty(IDList)) {
            ResponseStatus status =
                new ResponseStatus(Urlconstant.URL_BIG_DATA_19, VIIDRespEnum.OK.getCode(), new Date());
            setStatus(status, VIIDRespEnum.INVALID_JSON_CONTENT, "参数IDList为空；");
            resps.add(status);
            return BeanUtilss.getBatchResponseModel(resps);
        }

        String[] ids = IDList.split(",");
        for (String id : ids) {
            ResponseStatus status =
                new ResponseStatus(Urlconstant.URL_BIG_DATA_19, VIIDRespEnum.OK.getCode(), new Date());
            try {
                deleteCommon(status, id);
            } catch (Exception e) {
                setStatus(status, VIIDRespEnum.DEVICE_ERROR, "请联系管理员；");
                log.error(" TBigDataNonMotorVehicleController  deleteList  Exception {} ", e);
            }
            resps.add(status);
        }
        return BeanUtilss.getBatchResponseModel(resps);
    }

    /**
     * 条件查询
     *
     * @param request
     * @return
     */
    @GetMapping(value = "/NonMotorVehicles", produces = "application/VIID+JSON;charset=UTF-8")
    public Object getList(HttpServletRequest request) {
        Object attribute = request.getAttribute("map");
        NonMotorVehicleBatchParam param = null;
        if (attribute instanceof Map) {
            Map<String, Object> map = (Map<String, Object>)attribute;
            param = nonMotorVehicleService.getNonMotorVehicleList(map);
        }
        if (param == null) {
            return new ResponseEntity<Map<String, Object>>(new HashMap<String, Object>(), HttpStatus.NO_CONTENT);
        }
        return param;
    }

    /**
     * 单个查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/NonMotorVehicles/{id}", produces = "application/VIID+JSON;charset=UTF-8")
    public Object get(@PathVariable("id") String id) {
        NonMotorVehicleBatch NonMotorVehicle = new NonMotorVehicleBatch();
        try {
            NonMotorVehicle tBigDataNonMotorVehicle = nonMotorVehicleService.findByNonMotorVehicleID(id);
            NonMotorVehicle = BeanUtilss.transferObjectIgnoreCase(tBigDataNonMotorVehicle, NonMotorVehicle.class);
            if (tBigDataNonMotorVehicle != null && !StringUtils.isEmpty(tBigDataNonMotorVehicle.getNonMotorVehicleID())) {
                List<SubImageInfoVo> volist =
                    imageBusinessService.findSubImageInfoVo(tBigDataNonMotorVehicle.getNonMotorVehicleID(), type);
                NonMotorVehicle.setSubImageList(volist);
            }
        } catch (Exception e) {
            log.error("TBigDataNonMotorVehicleController get Exception {}", e);
        }
        if (NonMotorVehicle == null) {
            return new ResponseEntity<Map<String, Object>>(new HashMap<String, Object>(), HttpStatus.NO_CONTENT);
        }
        NonMotorVehicleParam param = new NonMotorVehicleParam();
        NonMotorVehicleModel model = BeanUtilss.transferObjectCommon(NonMotorVehicle, NonMotorVehicleModel.class);
        if (NonMotorVehicle.getSubImageList() != null && NonMotorVehicle.getSubImageList().size() > 0) {
            SubImageBatchParam subImageList = new SubImageBatchParam();
            subImageList.setSubImageInfoObject(NonMotorVehicle.getSubImageList());
            model.setSubImageList(subImageList);
        }
        param.setNonMotorVehicleObject(model);
        return param;
    }

    /**
     * 单个修改
     *
     * @param param
     * @param id
     * @return
     * @throws IOException
     * @throws JsonMappingException
     * @throws JsonParseException
     */
    @PutMapping(value = "/NonMotorVehicles/{id}", produces = "application/VIID+JSON")
    public Object put(@RequestBody NonMotorVehicleParam param, @PathVariable("id") String id) throws IOException {
        ResponseStatus status = new ResponseStatus(Urlconstant.URL_BIG_DATA_20, VIIDRespEnum.OK.getCode(), new Date());
        if (param == null || param.getNonMotorVehicleObject() == null) {
            setStatus(status, VIIDRespEnum.INVALID_JSON_CONTENT, "参数NonMotorVehicle无效，没有对于数据；");
            return BeanUtilss.getResponseModel(status);
        }

        if (StringUtils.isEmpty(id)) {
            setStatus(status, VIIDRespEnum.INVALID_JSON_CONTENT, "参数NonMotorVehicleID为空；");
            return BeanUtilss.getResponseModel(status);
        }
        NonMotorVehicleModel model = param.getNonMotorVehicleObject();
        NonMotorVehicleBatch NonMotorVehicle = BeanUtilss.transferObjectCommon(model, NonMotorVehicleBatch.class);
        if (model.getSubImageList() != null && model.getSubImageList()
            .getSubImageInfoObject() != null && model.getSubImageList().getSubImageInfoObject().size() > 0) {
            NonMotorVehicle.setSubImageList(model.getSubImageList().getSubImageInfoObject());
        }
        try {
            validateParams(NonMotorVehicle, status);
            if (status.getStatusCode().equals(VIIDRespEnum.OK.getCode())) {
                updateCommon(NonMotorVehicle, status, id);
            }
        } catch (Exception e) {
            log.error("TBigDataNonMotorVehicleController put Exception {}", e);
            setStatus(status, VIIDRespEnum.DEVICE_ERROR, "请联系管理员；");
        }
        return BeanUtilss.getResponseModel(status);
    }

    /**
     * 单个删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/NonMotorVehicles/{id}", produces = "application/VIID+JSON")
    public Object delete(@PathVariable("id") String id) {
        ResponseStatus status = new ResponseStatus(Urlconstant.URL_BIG_DATA_20, VIIDRespEnum.OK.getCode(), new Date());
        if (StringUtils.isEmpty(id)) {
            setStatus(status, VIIDRespEnum.INVALID_JSON_CONTENT, "参数NonMotorVehicleID为空；");
            return BeanUtilss.getResponseModel(status);
        }
        try {
            deleteCommon(status, id);
        } catch (Exception e) {
            log.error("TBigDataNonMotorVehicleController delete Exception {}", e);
            setStatus(status, VIIDRespEnum.DEVICE_ERROR, "请联系管理员；");
        }
        return BeanUtilss.getResponseModel(status);
    }

    public void deleteCommon(ResponseStatus status, String id) throws Exception {
        NonMotorVehicle TBigDataNonMotorVehicle2 = nonMotorVehicleService.findByNonMotorVehicleID(id);
        if (TBigDataNonMotorVehicle2 == null) {
            setStatus(status, VIIDRespEnum.INVALID_JSON_CONTENT, "参数NonMotorVehicleID无效，没有对于数据；");
        } else {
            nonMotorVehicleService.deleteByNonMotorVehicleID(id);
            status.setId(id);
            status.setStatusString("删除成功；");
        }
    }

    public void updateCommon(NonMotorVehicleBatch NonMotorVehicle, ResponseStatus status, String id) throws Exception {
        NonMotorVehicle TBigDataNonMotorVehicle2 = nonMotorVehicleService.findByNonMotorVehicleID(id);
        if (TBigDataNonMotorVehicle2 == null) {
            setStatus(status, VIIDRespEnum.INVALID_JSON_CONTENT, "参数NonMotorVehicleID无效，没有对于数据；");
        } else {
            NonMotorVehicle.setId(TBigDataNonMotorVehicle2.getId());
            NonMotorVehicle tBigDataNonMotorVehicle = BeanUtilss.transferObjectIgnoreCase(NonMotorVehicle, NonMotorVehicle.class);
            nonMotorVehicleService.updateById(tBigDataNonMotorVehicle);

            // 图片和业务对于关系
            saveImageBusiness(NonMotorVehicle.getSubImageList(), type, NonMotorVehicle.getNonMotorVehicleID());
            status.setId(NonMotorVehicle.getNonMotorVehicleID());
            status.setStatusString("修改成功；");
        }
    }

    /**
     * 设置返回值代码和消息 VIID 标准
     *
     * @param res
     * @param e
     * @param field
     */
    protected void setStatus(ResponseStatus res, VIIDRespEnum e, String field) {
        StringBuffer sb = new StringBuffer();
        sb.append(e.getMsg() + "[" + field + "]");
        res.setStatusCode(e.getCode());
        res.setStatusString(sb.toString());
    }

    public void saveImageBusiness(List<SubImageInfoVo> subImageList, String type, String ywId) {
        if (subImageList != null && !subImageList.isEmpty()) {
            for (SubImageInfoVo vo : subImageList) {
                if (vo == null || StringUtils.isEmpty(vo.getImageID())) {
                    continue;
                }
                ImageBusiness tBigDataImageBusiness = new ImageBusiness();
                tBigDataImageBusiness.setImageId(vo.getImageID());
                tBigDataImageBusiness.setYwId(ywId);
                tBigDataImageBusiness.setBigType(type);
                tBigDataImageBusiness.setImageType(type);

                if ("01".equals(type)) {
                    if (!StringUtils.isEmpty(vo.getType()) && Arrays.asList("01", "02", "03", "04", "05", "06", "07",
                        "08", "09").contains(vo.getType())) {
                        tBigDataImageBusiness.setImageType(vo.getType());
                    }
                }
                // 先删除
                deleteImageBusiness(type, ywId);
                // 新增
                imageBusinessService.save(tBigDataImageBusiness);
            }
        }
    }

    public void deleteImageBusiness(String type, String ywId) {
        ImageBusiness tBigDataImageBusiness = new ImageBusiness();
        tBigDataImageBusiness.setYwId(ywId);
        tBigDataImageBusiness.setBigType(type);
        imageBusinessService.deleteImageBusiness(tBigDataImageBusiness);
    }

}
